package com.ihighest.app.infox.activity;

import io.rong.imkit.RongIM;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.base.AbstractActivity;
import com.ihighest.app.common.core.Application;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.bean.ChannelItem;
import com.ihighest.app.infox.bean.ChannelManage;
import com.ihighest.app.infox.tools.CheckNetWork;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengRegistrar;
import com.umeng.message.proguard.k.e;
import com.umeng.update.UmengDialogButtonListener;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;

//git.oschina.net/ihighest/InfoX_Android_Native.git

public class SplashActivity extends AbstractActivity {
	Intent intent;
	static final String TAG = "SplashActivity";
	String json = "";
	private ArrayList<String> imgList;// 广告图片集合
	private ArrayList<String> actionValue;// 图片的web地址
	private boolean isSkim;// 是否允许访客访问
	String token;
	private JSONArray ids;

	private int get = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Application.getInstance().addActivity(this);
		setContentView(R.layout.infox_activity_splash);
		if (SharedPreferencesKit.getBoolean(getApplicationContext(), Const.SP_ACCEPTINFOMATION, true)) {
			SharedPreferencesKit.putBoolean(getApplicationContext(), Const.SP_ACCEPTINFOMATION, true);
			if (PushAgent.getInstance(getApplicationContext()).isEnabled()) {
				PushAgent.getInstance(getApplicationContext()).disable();
			}
			PushAgent.getInstance(getApplicationContext()).enable();
			Log.d(TAG, "RegistrationId --> " + UmengRegistrar.getRegistrationId(this));
		} else {
			if (PushAgent.getInstance(getApplicationContext()).isEnabled()) {
				PushAgent.getInstance(getApplicationContext()).disable();
			}
		}
		if (CheckNetWork.isConnected(SplashActivity.this)) {// 如果网络正常
			// 加载App配置

			getappsettingdata();

		} else {
			// 没有网络进入主界面
			// intent = new Intent(SplashActivity.this, LoginActivity.class);
			intent = new Intent(SplashActivity.this, MainActivity.class);
			startActivity(intent);
			finish();
		}
	}

	private void getappsettingdata() {
		// TODO Auto-generated method stub

		Api.data("app_setting.json", new ApiCallback(this, true) {

			@Override
			public void handleSuccess(ApiResult result) {
				try {
					final int minVerson = result.getData().getInt("a_min_version");

					if (AppKit.getAppVersionCode(mContext) < minVerson) {
						ToastKit.showShort(mContext, "您的App版本过低，请升级到最新版本！");
						// 版本检测
						UmengUpdateAgent.setUpdateOnlyWifi(false);// 非wifi下也能更新
						UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {

							@Override
							public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo) {
								switch (updateStatus) {
								case UpdateStatus.Yes: // has update
									UmengUpdateAgent.showUpdateDialog(getApplicationContext(), updateInfo);
									UmengUpdateAgent.setDialogListener(new UmengDialogButtonListener() {

										@Override
										public void onClick(int status) {
											switch (status) {
											case UpdateStatus.Update:
												SharedPreferencesKit.putBoolean(getApplicationContext(), Const.SP_FIRSTOPEN, false);
												break;
											case UpdateStatus.Ignore:
												finish();
												Application.getInstance().exit();
												System.exit(0);
												break;
											case UpdateStatus.NotNow:
												break;
											}
										}
									});
									break;
								case UpdateStatus.No: // has no update
									ToastKit.showShort(getApplicationContext(), "当前已经是最新版本");
									break;
								case UpdateStatus.NoneWifi: // none wifi
									ToastKit.showShort(getApplicationContext(), "当前没有Wifi连接，请在Wifi环境下进行更新");
									break;
								case UpdateStatus.Timeout: // time out
									ToastKit.showShort(getApplicationContext(), "检测更新超时，请查看当前网络是否正常");
									break;
								}
							}
						});
						UmengUpdateAgent.forceUpdate(getApplicationContext());
					} else {
						SharedPreferencesKit.putJsonObject(mContext, Const.SP_KEY_CONFIG_JSON, result.getData());
						final String uid = SharedPreferencesKit.getString(mContext, Const.SP_KEY_USER_ID);
						final String mid = SharedPreferencesKit.getString(mContext, Const.SP_KEY_USER_MID);
						// tokenxiaoyan
						if (TextUtils.isEmpty(uid)) {
							try {
								// 断开融云
								// RongIM.getInstance().disconnect();
								RongIM.getInstance().logout();
								// Umeng Push
								if (TextUtils.isEmpty(mid)) {
									new Thread() {

										@Override
										public void run() {
											try {
												PushAgent.getInstance(getApplicationContext()).removeAlias(mid, "MID");
											} catch (e e) {
												// TODO Auto-generated catch
												// block
												e.printStackTrace();
											} catch (JSONException e) {
												// TODO Auto-generated catch
												// block
												e.printStackTrace();
											} catch (Exception e) {
												// TODO Auto-generated catch
												// block
												e.printStackTrace();
											}

										}
									}.start();

								}
							} catch (Exception ex) {
								Log.e(TAG, ex.getMessage(), ex);
							}
							AppKit.cleanUserInfo(getApplicationContext());
						} else {
							Api.post("valid", ApiParam.create().addParam("uid", uid), new ApiCallback(SplashActivity.this, true) {

								@Override
								public void handleSuccess(ApiResult result) {
									// 效验成功
									if (result.getCode() == 100) {
										// 连接融云
										AppKit.connectRongIM(SplashActivity.this,
												SharedPreferencesKit.getString(mContext, Const.SP_KEY_USER_RTOKEN));
										new Thread(new Runnable() {

											@Override
											public void run() {
												try {
													PushAgent.getInstance(getApplicationContext()).addAlias(uid, "AID");
													PushAgent.getInstance(getApplicationContext()).addAlias(mid, "MID");
													if (SharedPreferencesKit.getBoolean(getApplicationContext(), Const.SP_ACCEPTINFOMATION,
															true)) {
														if (PushAgent.getInstance(getApplicationContext()).isEnabled()) {
															PushAgent.getInstance(getApplicationContext()).disable();
														}
														PushAgent.getInstance(getApplicationContext()).enable();
														Log.d(TAG, "RegistrationId --> " + UmengRegistrar.getRegistrationId(mContext));
													} else {
														if (PushAgent.getInstance(getApplicationContext()).isEnabled()) {
															PushAgent.getInstance(getApplicationContext()).disable();
														}
													}
												} catch (Exception ex) {
													Log.e(TAG, ex.getMessage(), ex);
												}

											}
										}).start();
									}
								}

								@Override
								public void handleFailure(Exception e, String msg) {

									if (!TextUtils.isEmpty(SharedPreferencesKit.getString(getApplicationContext(), Const.SP_KEY_USER_ID))) {

										new Thread(new Runnable() {

											@Override
											public void run() {
												try {
													// 断开融云
													// RongIM.getInstance().disconnect();
													RongIM.getInstance().logout();
													// Umeng Push
													new Thread() {

														@Override
														public void run() {
															try {
																PushAgent.getInstance(getApplicationContext()).removeAlias(uid, "AID");
																PushAgent.getInstance(getApplicationContext()).removeAlias(mid, "MID");
															} catch (e e) {

																e.printStackTrace();
															} catch (JSONException e) {

																e.printStackTrace();
															} catch (Exception e) {

																e.printStackTrace();
															}

														}
													}.start();

													AppKit.cleanUserInfo(getApplicationContext());
												} catch (Exception ex) {
													Log.e(TAG, ex.getMessage(), ex);
												}

											}
										}).start();
									}
								}
							}, SplashActivity.this);
						}
						try {
							isSkim = result.getData().getBoolean("guest_access_switch");

							initColumnData();
							if (isSkim) {

								// 如果允许用户进入
								start();
							} else {
								intent = new Intent(SplashActivity.this, LoginActivity.class);
								startActivity(intent);
								finish();
							}
						} catch (JSONException e) {
							Log.e(TAG, e.getMessage(), e);
						}
						SharedPreferencesKit.putString(SplashActivity.this, Const.SP_TEXTSIZE, "mid");
						if (!TextUtils.isEmpty(SharedPreferencesKit.getString(SplashActivity.this, Const.SP_KEY_USER_ID))) {

							// 用户已经登录，加载用户设置信息
							String url = "member/setting/" + SharedPreferencesKit.getString(SplashActivity.this, Const.SP_KEY_USER_ID)
									+ ".json";
							Api.data(url, new ApiCallback(SplashActivity.this, true) {

								@Override
								public void handleSuccess(ApiResult result) {
									try {
										if (result.getData() != null) {
											if (result.getData().has(Const.SP_NIGHTStyle)) {
												// 设置信息是否存在夜间模式
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_NIGHTStyle,
														Boolean.valueOf(result.getData().getString(Const.SP_NIGHTStyle)));

											} else {
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_NIGHTStyle, false);
											}
											if (result.getData().has(Const.SP_TEXTSIZE)) {
												// 设置信息是否存在字体设置
												SharedPreferencesKit.putString(SplashActivity.this, Const.SP_TEXTSIZE, result.getData()
														.getString(Const.SP_TEXTSIZE));
											} else {
												SharedPreferencesKit.putString(SplashActivity.this, Const.SP_TEXTSIZE, "mid");
											}
											if (result.getData().has(Const.SP_SLIDINGCHANGEPAGER)) {
												// 设置信息是否存在滑动切换文章
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_SLIDINGCHANGEPAGER,
														Boolean.valueOf(result.getData().getString(Const.SP_SLIDINGCHANGEPAGER)));
											} else {
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_SLIDINGCHANGEPAGER, false);
											}
											if (result.getData().has(Const.SP_WHEN_WIFI_SHOWIMG)) {
												// WiFi模式下显示图片
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_WHEN_WIFI_SHOWIMG,
														Boolean.valueOf(result.getData().getString(Const.SP_WHEN_WIFI_SHOWIMG)));

											} else {
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_WHEN_WIFI_SHOWIMG, true);
												Api.post("me/setting", ApiParam.create().addParam("key", Const.SP_WHEN_WIFI_SHOWIMG)
														.addParam("value", String.valueOf(true)),
														new ApiCallback(SplashActivity.this, true) {

															@Override
															public void handleSuccess(ApiResult result) {

															}

															@Override
															public void handleFailure(Exception e, String msg) {

															}
														}, SplashActivity.this);
											}
											if (result.getData().has(Const.SP_ACCEPTINFOMATION)) {
												// 是否接收通知
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_ACCEPTINFOMATION,
														Boolean.valueOf(result.getData().getString(Const.SP_ACCEPTINFOMATION)));

											} else {
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_ACCEPTINFOMATION, true);
												Api.post("me/setting", ApiParam.create().addParam("key", Const.SP_ACCEPTINFOMATION)
														.addParam("value", String.valueOf(true)),
														new ApiCallback(SplashActivity.this, true) {

															@Override
															public void handleSuccess(ApiResult result) {

															}

															@Override
															public void handleFailure(Exception e, String msg) {

															}
														}, SplashActivity.this);
											}
											if (result.getData().has(Const.SP_DINGYUE_SHARE)) {
												// 订阅时分享
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_DINGYUE_SHARE,
														Boolean.valueOf(result.getData().getString(Const.SP_DINGYUE_SHARE)));

											} else {
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_DINGYUE_SHARE, false);
											}
											if (result.getData().has(Const.SP_PAYATTENTIONSHARE)) {
												// 关注时分享
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_PAYATTENTIONSHARE,
														Boolean.valueOf(result.getData().getString(Const.SP_PAYATTENTIONSHARE)));

											} else {
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_PAYATTENTIONSHARE, false);
											}
											if (result.getData().has(Const.SP_COLLECTIONSHARE)) {
												// 收藏时分享
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_COLLECTIONSHARE,
														Boolean.valueOf(result.getData().getString(Const.SP_COLLECTIONSHARE)));

											} else {
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_COLLECTIONSHARE, false);
											}
											if (result.getData().has(Const.SP_ASSESSMENTSHARE)) {
												// 评论是分享
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_ASSESSMENTSHARE,
														Boolean.valueOf(result.getData().getString(Const.SP_ASSESSMENTSHARE)));

											} else {
												SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_ASSESSMENTSHARE, false);
											}

										}
									} catch (JSONException e) {
										e.printStackTrace();
									}
									if (SharedPreferencesKit.getBoolean(getApplicationContext(), Const.SP_ACCEPTINFOMATION, true)) {
										if (PushAgent.getInstance(getApplicationContext()).isEnabled()) {
											PushAgent.getInstance(getApplicationContext()).disable();
										}
										PushAgent.getInstance(getApplicationContext()).enable();
										Log.d(TAG, "RegistrationId --> " + UmengRegistrar.getRegistrationId(mContext));
									} else {
										if (PushAgent.getInstance(getApplicationContext()).isEnabled()) {
											PushAgent.getInstance(getApplicationContext()).disable();
										}
									}
								}

								@Override
								public void handleFailure(Exception e, String msg) {

								}
							}, SplashActivity.this);
						}

					}

				} catch (JSONException e1) {
					System.out.println("捕获异常");
					e1.printStackTrace();
				}

			}

			@Override
			public void handleFailure(Exception e, String msg) {
				ToastKit.showLong(SplashActivity.this, "网络异常");
				if (SharedPreferencesKit.getJsonObject(SplashActivity.this, Const.SP_KEY_CONFIG_JSON) != null) {
					fileConnect();
				} else {
					finish();
					Application.getInstance().exit();
					System.exit(0);
				}

				// intent = new Intent(SplashActivity.this, MainActivity.class);
				// startActivity(intent);
				// finish();
				// if(get<=10){
				//
				// getappsettingdata();
				// get++;
				// }else{
				//
				// finish();
				// Application.getInstance().exit();
				// System.exit(0);
				// }

			}
		}, this);
	}

	public void start() {
		if (!SharedPreferencesKit.getBoolean(this, Const.SP_FIRSTOPEN)
				|| SharedPreferencesKit.getInt(this, Const.SP_KEY_LAST_APP_VERSION, -1) < AppKit.getAppVersionCode(this)) {
			SharedPreferencesKit.putBoolean(this, Const.SP_FIRSTOPEN, true);
			SharedPreferencesKit.putInt(this, Const.SP_KEY_LAST_APP_VERSION, AppKit.getAppVersionCode(this));
			intent = new Intent(SplashActivity.this, GuideActivity.class);

			Const.INTO_GUIDE = true;
			startActivity(intent);
			finish();
		} else {

			// 判断广告数据是否有值
			Api.data("startad.json", new ApiCallback(this, true) {
				@Override
				public void handleSuccess(ApiResult result) {

					if (result.getDatas() != null && result.getDatas().length() > 0) {
						intent = new Intent(SplashActivity.this, AdvertisementActivity.class);
						imgList = new ArrayList<>();
						actionValue = new ArrayList<>();
						// 展示启动广告
						for (int i = 0; i < result.getDatas().length(); i++) {
							try {
								imgList.add(result.getDatas().getJSONObject(i).getString("image"));
								actionValue.add(result.getDatas().getJSONObject(i).getJSONObject("action").getString("value"));
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
						intent.putExtra("empty", false);
						intent.putStringArrayListExtra("imgs", imgList);
						intent.putStringArrayListExtra("actionValue", actionValue);
						startActivity(intent);
						finish();
					} else {
						// 进入主页
						// 如果有值显示广告，没值延迟1秒跳转
						new Handler().postDelayed(new Runnable() {
							@Override
							public void run() {
								intent = new Intent(SplashActivity.this, MainActivity.class);
								startActivity(intent);
								finish();
							}
						}, 500); // 延迟1秒跳转
					}

				}

				@Override
				public void handleFailure(Exception e, String msg) {
					intent = new Intent(SplashActivity.this, MainActivity.class);
					startActivity(intent);
					finish();
				}
			}, this);
		}
		SharedPreferencesKit.putInt(this, Const.SP_KEY_LAST_APP_VERSION, AppKit.getAppVersionCode(this));
	}

	private void initColumnData() {

		if (TextUtils.isEmpty(SharedPreferencesKit.getString(SplashActivity.this, Const.SP_KEY_USER_ID))) {

			Api.data("showchannel_list.json", new ApiCallback(SplashActivity.this, true) {

				@Override
				public void handleSuccess(ApiResult result) {

					for (int i = 0; i < result.getDatas().length(); i++) {
						try {
							String title = result.getDatas().getJSONObject(i).getString("title");
							String id = result.getDatas().getJSONObject(i).getString("id");
							String sort = result.getDatas().getJSONObject(i).getString("sort");
							String type = result.getDatas().getJSONObject(i).getString("type");
							String cancel = result.getDatas().getJSONObject(i).getString("cancel");

							ChannelManage.defaultUserChannels.add(new ChannelItem(id, title, i, 1, sort, type, cancel));

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}
					ChannelManage.getManage(Application.getInstance().getSQLHelper()).initDefaultChannel();

				}

				@Override
				public void handleFailure(Exception e, String msg) {

				}
			}, SplashActivity.this);

		} else {
			Api.get("me/info", ApiParam.create(), new ApiCallback(this, true) {

				@Override
				public void handleSuccess(ApiResult result) {
					try {
						ids = result.getData().getJSONArray("channel");
						SharedPreferencesKit.putJsonArray(getApplicationContext(), Const.TAGS, result.getData().getJSONArray("tags"));

						getallchannel();

					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

				@Override
				public void handleFailure(Exception e, String msg) {
					Log.e("me/info", msg);
				}
			}, this);

		}

	}

	protected void getallchannel() {

		Api.data("channel_list.json", new ApiCallback(SplashActivity.this, true) {

			@Override
			public void handleSuccess(ApiResult result) {
				ChannelManage.defaultUserChannels.clear();
				ChannelManage.defaultOtherChannels.clear();
				Const.unmove = 0;
				int a = 0;
				int b = 0;
				ArrayList<ChannelItem> list = new ArrayList<>();
				for (int i = 0; i < result.getDatas().length(); i++) {
					try {
						String id = result.getDatas().getJSONObject(i).getString("id");
						String title = result.getDatas().getJSONObject(i).getString("title");
						String type = result.getDatas().getJSONObject(i).getString("type");
						String cancel = result.getDatas().getJSONObject(i).getString("cancel");
						String sort = result.getDatas().getJSONObject(i).getString("sort");

						if (ids.length() == 0) {
							ChannelManage.defaultOtherChannels.add(new ChannelItem(id, title, b, 2, sort, type, cancel));
							System.out.println(b);
							b++;
						}

						for (int j = 0; j < ids.length(); j++) {
							if (id.equals(ids.get(j))) {

								if (cancel.equals("false")) {
									Const.unmove++;
								}

								list.add(new ChannelItem(id, title, a, 1, sort, type, cancel));
								a++;
								break;
							} else {
								if (j == ids.length() - 1) {

									ChannelManage.defaultOtherChannels.add(new ChannelItem(id, title, b, 2, sort, type, cancel));
									b++;
								}
							}

						}

					} catch (JSONException e) {
						e.printStackTrace();
					}

				}

				for (int i = 0; i < ids.length(); i++) {
					for (int j = 0; j < list.size(); j++) {
						try {
							if (ids.get(i).equals(list.get(j).getId())) {
								ChannelManage.defaultUserChannels.add(list.get(j));

							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}

				}

				ChannelManage.getManage(Application.getInstance().getSQLHelper()).initDefaultChannel();

			}

			@Override
			public void handleFailure(Exception e, String msg) {
			}
		}, SplashActivity.this);

	}

	public void fileConnect() {
		JSONObject jsonObj = SharedPreferencesKit.getJsonObject(SplashActivity.this, Const.SP_KEY_CONFIG_JSON);
		try {
			final int minVerson = jsonObj.getInt("a_min_version");

			if (AppKit.getAppVersionCode(SplashActivity.this) < minVerson) {
				ToastKit.showShort(SplashActivity.this, "您的App版本过低，请升级到最新版本！");
				// 版本检测
				UmengUpdateAgent.setUpdateOnlyWifi(false);// 非wifi下也能更新
				UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {

					@Override
					public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo) {
						switch (updateStatus) {
						case UpdateStatus.Yes: // has update
							UmengUpdateAgent.showUpdateDialog(getApplicationContext(), updateInfo);
							UmengUpdateAgent.setDialogListener(new UmengDialogButtonListener() {

								@Override
								public void onClick(int status) {
									switch (status) {
									case UpdateStatus.Update:
										SharedPreferencesKit.putBoolean(getApplicationContext(), Const.SP_FIRSTOPEN, false);
										break;
									case UpdateStatus.Ignore:
										finish();
										Application.getInstance().exit();
										System.exit(0);
										break;
									case UpdateStatus.NotNow:
										break;
									}
								}
							});
							break;
						case UpdateStatus.No: // has no update
							ToastKit.showShort(getApplicationContext(), "当前已经是最新版本");
							break;
						case UpdateStatus.NoneWifi: // none wifi
							ToastKit.showShort(getApplicationContext(), "当前没有Wifi连接，请在Wifi环境下进行更新");
							break;
						case UpdateStatus.Timeout: // time out
							ToastKit.showShort(getApplicationContext(), "检测更新超时，请查看当前网络是否正常");
							break;
						}
					}
				});
				UmengUpdateAgent.forceUpdate(getApplicationContext());
			} else {

				final String uid = SharedPreferencesKit.getString(SplashActivity.this, Const.SP_KEY_USER_ID);
				final String mid = SharedPreferencesKit.getString(SplashActivity.this, Const.SP_KEY_USER_MID);
				// tokenxiaoyan
				if (TextUtils.isEmpty(uid)) {
					new Thread() {

						@Override
						public void run() {
							try {
								// 断开融云
								// RongIM.getInstance().disconnect();
								RongIM.getInstance().logout();
								// Umeng Push
								if (TextUtils.isEmpty(mid)) {
									PushAgent.getInstance(getApplicationContext()).removeAlias(mid, "MID");
								}
							} catch (Exception ex) {
								Log.e(TAG, ex.getMessage(), ex);
							}
						}
					}.start();

					AppKit.cleanUserInfo(getApplicationContext());
				} else {
					Api.post("valid", ApiParam.create().addParam("uid", uid), new ApiCallback(SplashActivity.this, true) {

						@Override
						public void handleSuccess(ApiResult result) {
							// 效验成功
							if (result.getCode() == 100) {
								// 连接融云
								AppKit.connectRongIM(SplashActivity.this,
										SharedPreferencesKit.getString(mContext, Const.SP_KEY_USER_RTOKEN));
								new Thread(new Runnable() {

									@Override
									public void run() {
										try {
											PushAgent.getInstance(getApplicationContext()).addAlias(uid, "AID");
											PushAgent.getInstance(getApplicationContext()).addAlias(mid, "MID");
											if (SharedPreferencesKit.getBoolean(getApplicationContext(), Const.SP_ACCEPTINFOMATION, true)) {
												if (PushAgent.getInstance(getApplicationContext()).isEnabled()) {
													PushAgent.getInstance(getApplicationContext()).disable();
												}
												PushAgent.getInstance(getApplicationContext()).enable();
												Log.d(TAG, "RegistrationId --> " + UmengRegistrar.getRegistrationId(mContext));
											} else {
												if (PushAgent.getInstance(getApplicationContext()).isEnabled()) {
													PushAgent.getInstance(getApplicationContext()).disable();
												}
											}
										} catch (Exception ex) {
											Log.e(TAG, ex.getMessage(), ex);
										}

									}
								}).start();
							}
						}

						@Override
						public void handleFailure(Exception e, String msg) {

							if (!TextUtils.isEmpty(SharedPreferencesKit.getString(getApplicationContext(), Const.SP_KEY_USER_ID))) {

								new Thread(new Runnable() {

									@Override
									public void run() {
										try {
											// 断开融云
											// RongIM.getInstance().disconnect();
											RongIM.getInstance().logout();
											// Umeng Push
											PushAgent.getInstance(getApplicationContext()).removeAlias(uid, "AID");
											PushAgent.getInstance(getApplicationContext()).removeAlias(mid, "MID");
											AppKit.cleanUserInfo(getApplicationContext());
										} catch (Exception ex) {
											Log.e(TAG, ex.getMessage(), ex);
										}

									}
								}).start();
							}
						}
					}, SplashActivity.this);
				}
				try {
					isSkim = jsonObj.getBoolean("guest_access_switch");

					initColumnData();
					if (isSkim) {

						// 如果允许用户进入
						start();
					} else {
						intent = new Intent(SplashActivity.this, LoginActivity.class);
						startActivity(intent);
						finish();
					}
				} catch (JSONException e) {
					Log.e(TAG, e.getMessage(), e);
				}
				SharedPreferencesKit.putString(SplashActivity.this, Const.SP_TEXTSIZE, "mid");
				if (!TextUtils.isEmpty(SharedPreferencesKit.getString(SplashActivity.this, Const.SP_KEY_USER_ID))) {

					// 用户已经登录，加载用户设置信息
					String url = "member/setting/" + SharedPreferencesKit.getString(SplashActivity.this, Const.SP_KEY_USER_ID) + ".json";
					Api.data(url, new ApiCallback(SplashActivity.this, true) {

						@Override
						public void handleSuccess(ApiResult result) {
							try {
								if (result.getData() != null) {
									if (result.getData().has(Const.SP_NIGHTStyle)) {
										// 设置信息是否存在夜间模式
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_NIGHTStyle,
												Boolean.valueOf(result.getData().getString(Const.SP_NIGHTStyle)));

									} else {
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_NIGHTStyle, false);
									}
									if (result.getData().has(Const.SP_TEXTSIZE)) {
										// 设置信息是否存在字体设置
										SharedPreferencesKit.putString(SplashActivity.this, Const.SP_TEXTSIZE,
												result.getData().getString(Const.SP_TEXTSIZE));
									} else {
										SharedPreferencesKit.putString(SplashActivity.this, Const.SP_TEXTSIZE, "mid");
									}
									if (result.getData().has(Const.SP_SLIDINGCHANGEPAGER)) {
										// 设置信息是否存在滑动切换文章
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_SLIDINGCHANGEPAGER,
												Boolean.valueOf(result.getData().getString(Const.SP_SLIDINGCHANGEPAGER)));
									} else {
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_SLIDINGCHANGEPAGER, false);
									}
									if (result.getData().has(Const.SP_WHEN_WIFI_SHOWIMG)) {
										// WiFi模式下显示图片
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_WHEN_WIFI_SHOWIMG,
												Boolean.valueOf(result.getData().getString(Const.SP_WHEN_WIFI_SHOWIMG)));

									} else {
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_WHEN_WIFI_SHOWIMG, true);
										Api.post(
												"me/setting",
												ApiParam.create().addParam("key", Const.SP_WHEN_WIFI_SHOWIMG)
														.addParam("value", String.valueOf(true)),
												new ApiCallback(SplashActivity.this, true) {

													@Override
													public void handleSuccess(ApiResult result) {

													}

													@Override
													public void handleFailure(Exception e, String msg) {

													}
												}, SplashActivity.this);
									}
									if (result.getData().has(Const.SP_ACCEPTINFOMATION)) {
										// 是否接收通知
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_ACCEPTINFOMATION,
												Boolean.valueOf(result.getData().getString(Const.SP_ACCEPTINFOMATION)));

									} else {
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_ACCEPTINFOMATION, true);
										Api.post(
												"me/setting",
												ApiParam.create().addParam("key", Const.SP_ACCEPTINFOMATION)
														.addParam("value", String.valueOf(true)),
												new ApiCallback(SplashActivity.this, true) {

													@Override
													public void handleSuccess(ApiResult result) {

													}

													@Override
													public void handleFailure(Exception e, String msg) {

													}
												}, SplashActivity.this);
									}
									if (result.getData().has(Const.SP_DINGYUE_SHARE)) {
										// 订阅时分享
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_DINGYUE_SHARE,
												Boolean.valueOf(result.getData().getString(Const.SP_DINGYUE_SHARE)));

									} else {
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_DINGYUE_SHARE, false);
									}
									if (result.getData().has(Const.SP_PAYATTENTIONSHARE)) {
										// 关注时分享
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_PAYATTENTIONSHARE,
												Boolean.valueOf(result.getData().getString(Const.SP_PAYATTENTIONSHARE)));

									} else {
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_PAYATTENTIONSHARE, false);
									}
									if (result.getData().has(Const.SP_COLLECTIONSHARE)) {
										// 收藏时分享
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_COLLECTIONSHARE,
												Boolean.valueOf(result.getData().getString(Const.SP_COLLECTIONSHARE)));

									} else {
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_COLLECTIONSHARE, false);
									}
									if (result.getData().has(Const.SP_ASSESSMENTSHARE)) {
										// 评论是分享
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_ASSESSMENTSHARE,
												Boolean.valueOf(result.getData().getString(Const.SP_ASSESSMENTSHARE)));

									} else {
										SharedPreferencesKit.putBoolean(SplashActivity.this, Const.SP_ASSESSMENTSHARE, false);
									}

								}
							} catch (JSONException e) {
								e.printStackTrace();
							}
							if (SharedPreferencesKit.getBoolean(getApplicationContext(), Const.SP_ACCEPTINFOMATION, true)) {
								if (PushAgent.getInstance(getApplicationContext()).isEnabled()) {
									PushAgent.getInstance(getApplicationContext()).disable();
								}
								PushAgent.getInstance(getApplicationContext()).enable();
								Log.d(TAG, "RegistrationId --> " + UmengRegistrar.getRegistrationId(mContext));
							} else {
								if (PushAgent.getInstance(getApplicationContext()).isEnabled()) {
									PushAgent.getInstance(getApplicationContext()).disable();
								}
							}
						}

						@Override
						public void handleFailure(Exception e, String msg) {

						}
					}, SplashActivity.this);
				}

			}

		} catch (JSONException e1) {
			System.out.println("捕获异常");
			e1.printStackTrace();
		}

	}
}
